package com.fundboss.display;

import model.market.*;
import model.trader.*;

public class MarketRunner {

	public static model.market.ModelResults runModel(MarketConfig config, MarketListener listener,
			TraderConfig traderConfig) {

		MarketManager manager = new MarketManager();
		if (config != null) {
			manager.setConfig(config);

		}

		if (listener != null) {
			manager.setMarketListener(listener);
		}

		int liquidity = config.getLiquidity();

		// Position pos = new Position(1000000);
		// Investor invest = new Investor(1);
		// Trade buy = new Trade(invest,0,50000);
		// Execution execution = new Execution(50,buy,null,50000);
		// pos.addTrade(buy);
		// manager.register(invest, pos);
		// if(pos.getExecutedQuantity(0)==0){
		// throw new RuntimeException();
		// }

		switch (traderConfig.getNumberOfInvestors()) {
		case TraderConfig.INVESTORS_MANY:
			manager.register(new Investor(1), new Position(500000));
			manager.register(new model.trader.Investor(1), new Position(1000000));
			manager.register(new model.trader.Investor(1), new Position(1000000));
			manager.register(new model.trader.Investor(1), new Position(1000000));
			liquidity = (int) (1.2 * liquidity);
		case TraderConfig.INVESTORS_FEW:
			manager.register(new model.trader.Investor(1), new Position(500000));
			manager.register(new model.trader.Investor(1), new Position(1000000));
			liquidity = (int) (1.1 * liquidity);
		case TraderConfig.INVESTORS_ONE:
			manager.register(new model.trader.Investor(1), new Position(1000000));
			
		}
		
		if(traderConfig.isIncludeIndexTracker()){
			manager.register(new model.trader.IndexTracker(),new Position(1000000));
		}
		
		if(traderConfig.isIncludeMomentumTrader()){
			manager.register(new MomentumTrader(), new Position(1000000));
		}
		

		// manager.register(new MomentumTrader(1,3),new Position(200000));
		// manager.register(new MomentumTrader(1,4),new Position(200000));

		// manager.register(new MarketMaker(.003,200),new Position(30000000));
		// manager.register(new MarketMaker(.005,500),new Position(30000000));
		manager.register(new MarketMaker(.01, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.02, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.03, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.04, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.05, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.06, liquidity), new Position(300000000));
		manager.register(new MarketMaker(.07, liquidity), new Position(300000000));

		for (double shift = .1; shift < 1; shift += .05) {
			manager.register(new MarketMaker(shift, 2 * liquidity), new Position(30000000));
		}

		// //manager.register(new MarketMaker(.04,1000),new Position(30000000));
		// manager.register(new MarketMaker(.05,1000),new Position(30000000));
		// //manager.register(new MarketMaker(.06,1000),new Position(30000000));
		// manager.register(new MarketMaker(.07,1000),new Position(30000000));
		// //manager.register(new MarketMaker(.08,1000),new Position(30000000));
		// manager.register(new MarketMaker(.09,1000),new Position(30000000));
		// manager.register(new MarketMaker(.1,2000),new Position(30000000));
		// //manager.register(new MarketMaker(.12,2000),new Position(30000000));
		// manager.register(new MarketMaker(.14,3000),new Position(30000000));
		// //manager.register(new MarketMaker(.16,3000),new Position(30000000));
		// manager.register(new MarketMaker(.18,3000),new Position(30000000));
		// manager.register(new MarketMaker(.2,5000),new Position(30000000));
		// manager.register(new MarketMaker(.25,5000),new Position(30000000));
		// manager.register(new MarketMaker(.3,5000),new Position(30000000));
		// manager.register(new MarketMaker(.4,10000),new Position(30000000));
		// manager.register(new MarketMaker(.5,20000),new Position(30000000));
		// manager.register(new MarketMaker(.65,200000),new Position(30000000));
		// manager.register(new MarketMaker(.85,400000),new Position(30000000));
		// //this guy is the buffer that stops our market blowin up..
		// manager.register(new MarketMaker(1,1000000),new Position(30000000));
		// //manager.register(new Opportunist(1.5,1000000),new
		// Position(30000000));
		// manager.register(new MarketMaker(2,10000000),new Position(30000000));
		// manager.register(new MarketMaker(4,10000000),new Position(30000000));
		manager.register(new MarketMaker(6, 100000000), new Position(30000000));

		try {
			ModelResults execute = manager.execute();
			listener.complete(execute);
			return execute;
		} catch (model.market.MarketException e) {
			e.printStackTrace();
			listener.error(e);
			throw e;
		} catch (RuntimeException e) {
			listener.error(e);
			throw e;
		}
	}

}
